//
//  SEGHouseholdPraiseViewModel.m
//  Commom
//
//  Created by Clavis on 2020/6/1.
//

#import "SEGHouseholdPraiseViewModel.h"
#import <SEGModel/SEGDigitalManager.h>

@interface SEGHouseholdPraiseViewModel ()

@property (nonatomic, assign) __block NSInteger pageIndex;
@property (nonatomic, strong) SEGDigitalManager *digitalManager;
@property (nonatomic, strong) RACSignal *refreshSignal;

@end

@implementation SEGHouseholdPraiseViewModel

- (RACSignal *)refreshRequestSignal:(BOOL)isRefresh {
    if (isRefresh) {
        self.pageIndex = 1;
    }
    return self.refreshSignal;
}

- (RACSignal *)refreshSignal {
    if (!_refreshSignal) {
        WeakSelf;
        _refreshSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [self.digitalManager requestFiveStarWithquizTypeId:@"10010" communityId:kAccountCommunityID pageNo:self.pageIndex pageSize:10 Finished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    weakSelf.pageIndex ++;
                    [subscriber sendNext: weakSelf.digitalManager.dataQuizSource];
                } else {
                    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                }
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }
    return _refreshSignal;
}

- (SEGDigitalManager *)digitalManager {
    if (!_digitalManager) {
        _digitalManager = [[SEGDigitalManager alloc] init];
    }
    return _digitalManager;
}

@end
